package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.entity.misc.Qinfo;
import cn.buk.tms.entity.misc.QinfoNotice;
import cn.buk.tms.sc.SearchQinfoCriteria;
import cn.buk.qms.service.FlightCharterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @author yfdai
 * @date 2017/1/8
 */
@RestController
@RequestMapping("/qinfoes")
public class QinfoController extends BaseController {

    @Autowired
    private FlightCharterService flightCharterService;

    @InitBinder("qinfoNotice")
    public void initBinder2(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("qinfoNotice.");
    }

    @RequestMapping("/")
    public CommonDto<Qinfo> searchQinfo(HttpServletRequest request,
                                        @ModelAttribute("sc") SearchQinfoCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);

        List<Qinfo> qinfos = flightCharterService.searchQinfo(enterpriseId, sc);

        CommonDto<Qinfo> qinfoDto = new CommonDto<>();
        qinfoDto.setDataList(qinfos);
        qinfoDto.setPage(sc.getPage());

        return qinfoDto;
    }

    @RequestMapping("/reInformQinfo.do")
    public JsonResult reInformQinfo(HttpServletRequest request, @RequestParam("id") int id) {
        final int enterpriseId = getEnterpriseId(request);

        return flightCharterService.reInformQinfo(enterpriseId, id);
    }

    @RequestMapping("/settings")
    public CommonDto<QinfoNotice> searchQinfoNotice(HttpServletRequest request,
                                                    @ModelAttribute("sc") CommonSearchCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);

        List<QinfoNotice> notices = flightCharterService.searchQinfoNotice(enterpriseId, sc);

        CommonDto<QinfoNotice> qinfoNoticeDto = new CommonDto<>();
        qinfoNoticeDto.setDataList(notices);
        qinfoNoticeDto.setPage(sc.getPage());

        return qinfoNoticeDto;
    }

    @RequestMapping("/settings/save")
    public JsonResult createQinfoNotice(HttpServletRequest request,
                                        @ModelAttribute("qinfoNotice") QinfoNotice qinfoNotice) {
        final int enterpriseId = getEnterpriseId(request);

        return flightCharterService.createQinfoNotice(enterpriseId, qinfoNotice);
    }

    @RequestMapping("/settings/{id}/deleted")
    public JsonResult deleteQinfoNotice(HttpServletRequest request,
                                        @PathVariable("id") int id) {
        final int enterpriseId = getEnterpriseId(request);

        return flightCharterService.deleteQinfoNotice(enterpriseId, id);
    }
}
